package com.sunzhe.cms.tag;

import com.ibeetl.cms.entity.CmsCatalog;
import com.ibeetl.cms.service.CmsCatalogService;
import com.sunzhe.cms.web.query.CmsCatalogQuery;
import org.apache.commons.lang3.StringUtils;
import org.beetl.core.tag.GeneralVarTagBinding;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype") // Tag是每次使用功能都会创建，因此使用prototype
public class CatalogListTag extends GeneralVarTagBinding {

	@Autowired
    CmsCatalogService catalogCmsService;

	@Override
	public void render() {

		if (this.getAttributeValue("id") != null) {//id 不为空时，只返回对象
			String id = String.valueOf(this.getAttributeValue("id"));
			CmsCatalog catalog = catalogCmsService.queryById(id);
			this.binds(catalog);
			this.doBodyRender();
			return;
		}
		
		
		CmsCatalogQuery condition = new CmsCatalogQuery();
		
		String pId = String.valueOf(this.getAttributeValue("parentCatalogId"));
		String type = String.valueOf(this.getAttributeValue("type"));
		String position = String.valueOf(this.getAttributeValue("position"));
		String code = String.valueOf(this.getAttributeValue("code"));
		String orderby = String.valueOf(this.getAttributeValue("orderby"));

		if (StringUtils.isNotBlank(type)) {
			condition.setType(type);
		}
		if (StringUtils.isNotBlank(code)) {
			condition.setCode(code);
		}
		if (StringUtils.isNotBlank(orderby)) {
			condition.setOrderby(orderby);
		}
		
		
		PageQuery<CmsCatalog> page = condition.getPageQuery();
		catalogCmsService.queryByCondition(page);
		this.binds(page);// 标签体上必须有var="" 的属性
		this.doBodyRender();
	}

}
